package com.orange.lockscreensettings;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.KeyEvent;

import com.orange.lockscreensettings.common.Constants;
import com.orange.lockscreensettings.common.LogHelper;
import com.orange.lockscreensettings.common.SettingsUtils;

public class PreferenceSettingActivity extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {
	private static final String	TAG								= "PreferenceSettingActivity";

	private static final String	KEY_USE_HOMESCREEN_WALLPAPER	= "preference_use_homescreen_wallpaper";
	private static final String	KEY_LOCKSCREEN_WALLPAPER		= "preference_lockscreen_wallpaper";
	private static final String	KEY_SHOW_NOTIFICATION			= "preference_widget_events";
	private static final String	KEY_NOTIFICATION_TYPE			= "preference_notification_type";
	private static final String	KEY_EVENT_PRIVACY				= "preference_widget_event_detail";
	private static final String	KEY_NOTIFICATION_SCREEN			= "preference_widget_event_notification_screen";
	private static final String	KEY_DISABLE_LOCKSCREEN			= "preference_disable_lockscreen";

	private static final String	ACTION_SHOW_DETAIL_SETTING		= "com.lockscreen.sms.show_detail_setting";
	public static final String	ISFROMPRESETTINGSACT			= "fromPreSettingsAct";

	private SharedPreferences	mSharedPreferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getPreferenceManager().setSharedPreferencesName(
				Constants.PREFERENCES_SETTINGS_NAME);
		getPreferenceManager().setSharedPreferencesMode(MODE_WORLD_READABLE);
		// Load the preferences from an XML resource
		addPreferencesFromResource(R.xml.setting);

		getPreferenceManager().getSharedPreferences()
				.registerOnSharedPreferenceChangeListener(this);

		SettingsUtils.init(this);

		mSharedPreferences = getSharedPreferences(
				Constants.PREFERENCES_SETTINGS_NAME, MODE_WORLD_WRITEABLE);
	}

	@Override
	protected void onResume() {
		super.onResume();

		updatePreference(KEY_USE_HOMESCREEN_WALLPAPER, mSharedPreferences);
		updatePreference(KEY_SHOW_NOTIFICATION, mSharedPreferences);
		updatePreference(KEY_DISABLE_LOCKSCREEN, mSharedPreferences);
	}

	@Override
	protected void onDestroy() {
		Intent intent = new Intent();
		intent.setAction(ACTION_SHOW_DETAIL_SETTING);
		sendBroadcast(intent);

		boolean isNtfcExist = SettingsUtils.isPackageExist(this,
				Constants.PKG_LOCKSCREEN_SERVICE);
		if (!isNtfcExist) {
			Editor editor = mSharedPreferences.edit();
			editor.remove(KEY_NOTIFICATION_SCREEN);
			editor.commit();
		}

		getPreferenceManager().getSharedPreferences()
				.unregisterOnSharedPreferenceChangeListener(this);

		super.onDestroy();
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		updatePreference(key, sharedPreferences);
	}

	@Override
	protected void onPause() {
		super.onPause();
		LogHelper.d(TAG, "onPause. ");
	}

	@Override
	protected void onStop() {
		super.onStop();
		LogHelper.d(TAG, "onStop. ");
	}

	private void updatePreference(String key,
			SharedPreferences sharedPreferences) {
		if (sharedPreferences != null) {
			boolean isNtfcExist = SettingsUtils.isPackageExist(this,
					Constants.PKG_NOTIFICATION_SUMMARY);
			if (key.equals(KEY_USE_HOMESCREEN_WALLPAPER)) {
				boolean enable = sharedPreferences.getBoolean(key, true);
				Preference preference = findPreference(KEY_LOCKSCREEN_WALLPAPER);
				preference.setEnabled(!enable);
			} else if (key.equals(KEY_SHOW_NOTIFICATION)) {
				boolean enable = sharedPreferences.getBoolean(key, true);

				Preference showNotifictionPrefrence = findPreference(KEY_SHOW_NOTIFICATION);
				CheckBoxPreference showNotifictionCheckBoxPrefrence = (CheckBoxPreference) findPreference(KEY_SHOW_NOTIFICATION);

				Preference notifictionTpyesPreference = findPreference(KEY_NOTIFICATION_TYPE);

				Preference privacyPreference = findPreference(KEY_EVENT_PRIVACY);
				CheckBoxPreference privacyCheckBoxPreference = (CheckBoxPreference) findPreference(KEY_EVENT_PRIVACY);

				Preference notifictionSummarySrceenPreference = findPreference(KEY_NOTIFICATION_SCREEN);
				CheckBoxPreference notifictionSummarySrceenCheckBoxPreference = (CheckBoxPreference) findPreference(KEY_NOTIFICATION_SCREEN);

				if (isNtfcExist) {
					showNotifictionPrefrence.setEnabled(true);
					notifictionTpyesPreference.setEnabled(enable);
					privacyPreference.setEnabled(enable);
					notifictionSummarySrceenPreference.setEnabled(enable);
					if (!enable) {
						privacyCheckBoxPreference.setChecked(false);
						notifictionSummarySrceenCheckBoxPreference
								.setChecked(false);
					}
				} else {
					showNotifictionPrefrence.setEnabled(false);
					showNotifictionCheckBoxPrefrence.setChecked(false);

					notifictionTpyesPreference.setEnabled(false);

					privacyPreference.setEnabled(false);
					privacyCheckBoxPreference.setChecked(false);

					notifictionSummarySrceenPreference.setEnabled(false);
					notifictionSummarySrceenCheckBoxPreference
							.setChecked(false);
				}
			} else if (key.equals(KEY_DISABLE_LOCKSCREEN)) {
				int unlockPatternEnabled = 0;
				try {
					unlockPatternEnabled = Settings.System.getInt(
							getContentResolver(),
							Settings.System.LOCK_PATTERN_ENABLED);
				} catch (SettingNotFoundException e) {
				}
				boolean enabled = unlockPatternEnabled > 0 ? true : false;
				CheckBoxPreference dlPreference = (CheckBoxPreference) findPreference(KEY_DISABLE_LOCKSCREEN);
				dlPreference.setEnabled(enabled);
				if (!enabled) {
					dlPreference.setChecked(enabled);
				}
			}

		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (KeyEvent.KEYCODE_BACK == keyCode) {
			Intent intent = new Intent();
			intent.setAction("com.orange.lockscreen.pre_SETTINGS");
			intent.putExtra(ISFROMPRESETTINGSACT, "true");
			LogHelper.d(TAG, "onKeyDown. KEYCODE_BACK");
			startActivity(intent);
			this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}

}
